package com.edge.pbx.view.xover;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.edge.pbxdsp.R;
import com.hanlink.dsp.ui.widget.XoverEditText;
import com.qztech.utils.YzDebug;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class PbxXoverContentLayout extends ViewGroup {

    public PbxXoverContentLayout(Context context, AttributeSet attrs) {

        super(context, attrs);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
        View view = null;
        int size = Math.min((int)(width*0.93f), (int)(height*0.93f));
        measuredChileSquare(R.id.pbx_delay_output_select_layout_wrapper, size);

        size = (int) Math.min((width - size)*0.4f, height*0.75f);
        measuredChileSquare(R.id.pbx_xover_seekbar_high, size);
        measuredChileSquare(R.id.pbx_xover_seekbar_low, size);



        //320  809
        int w = (int) (size*0.4f);
        int h = (int) (w * 0.9f);
        measuredChild(R.id.pbx_xover_whv_high, w, h);
        measuredChild(R.id.pbx_xover_whv_low, w, h);

        w = (int) (size * 1.2f);
        h = (int) (w * 0.11f);
        measuredChild(R.id.pbx_xover_high_btn_layout, w, h);
        measuredChild(R.id.pbx_xover_low_btn_layout, w, h);

        w = (int) (size * 0.3173f);
        h = (int) (size * 0.1328f);
        measuredChild(R.id.pbx_xover_hight_et, w, h);
        measuredChild(R.id.pbx_xover_low_et, w, h);


        setMeasuredDimension(width, height);
    }

    private void measuredChileSquare(int id, int size){
        final View view = findViewById(id);
        int spec = MeasureSpec.makeMeasureSpec(size,MeasureSpec.EXACTLY);
        view.measure(spec, spec);
    }
    private void measuredChild(int id, int width, int height){
        final View view = findViewById(id);
        if (view == null)return;
        view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }
    private int mCenterLeft = 0;

    @Override
    protected void dispatchDraw(Canvas canvas) {
//        canvas.drawColor(Color.RED);
        super.dispatchDraw(canvas);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;
        int w = 0, h = 0, cl, ct;
        View view = null;
        view = findViewById(R.id.pbx_delay_output_select_layout_wrapper);
        if (view != null){
            w = view.getMeasuredWidth();
            h = view.getMeasuredHeight();
            mCenterLeft = (width - w) / 2;
            t = (height - h) / 2;
            view.layout(mCenterLeft, t, mCenterLeft + w, t + h);
        }

        view = findViewById(R.id.pbx_xover_seekbar_high);
        if (view != null){
            w = view.getMeasuredWidth();
            h = view.getMeasuredHeight();
            l = (mCenterLeft - w)/3*2;
            t = (height - h) /2;
            view.layout(l, t, l+w, t +h);
        }
        if (view instanceof  XoverFreqSeekBar){
            RectF inputRect = ((XoverFreqSeekBar) view).getInputRect();
            XoverEditText et = findViewById(R.id.pbx_xover_hight_et);
            if (et != null){
                int etl = (int) (view.getLeft()+inputRect.left);
                int ett = (int) (view.getTop() + inputRect.top);
                et.setTextSize(inputRect.height()*0.2f);
                et.layout(etl, ett, (int)(etl + inputRect.width()),(int)(ett+inputRect.height()));
            }


        }
        layoutChileCenterView(R.id.pbx_xover_whv_high,
                w, l,t + h *0.46f);
        layoutChileCenterView(R.id.pbx_xover_high_btn_layout,
                w, l,t + h *0.95f);

        //=================================

        view = findViewById(R.id.pbx_xover_seekbar_low);
        if (view != null){
            w = view.getMeasuredWidth();
            h = view.getMeasuredHeight();
            l = width - (mCenterLeft - w)/3*2 -w ;
            t = (height - h) /2;
            view.layout(l, t, l+w, t +h);
        }
        if (view instanceof  XoverFreqSeekBar){
            RectF inputRect = ((XoverFreqSeekBar) view).getInputRect();
            XoverEditText et = findViewById(R.id.pbx_xover_low_et);
            if (et != null){
                int etl = (int) (view.getLeft()+inputRect.left);
                int ett = (int) (view.getTop() + inputRect.top);
                et.setTextSize(inputRect.height()*0.2f);
                et.layout(etl, ett, (int)(etl + inputRect.width()),(int)(ett+inputRect.height()));
            }

        }
        layoutChileCenterView(R.id.pbx_xover_whv_low,
                w, l,t + h *0.46f);
        layoutChileCenterView(R.id.pbx_xover_low_btn_layout,
                w, l,t + h *0.95f);


    }
    private void layoutChileCenterView(int id, int fWidth, int fLeft, float vTop){
        View view = findViewById(id);
        if (view == null)return;
        int cw = view.getMeasuredWidth();
        int ch = view.getMeasuredHeight();
        fLeft = fLeft + (fWidth - cw)/2;
        view.layout(fLeft, (int) vTop, fLeft+cw, (int)(vTop+ch));

    }
}
